package com.esender.search.mq;

import com.esender.common.constant.RabbitMQConstants;
import com.esender.common.constant.SearchConstant;
import com.esender.common.model.StandardSubmit;
import com.esender.common.util.JsonUtil;
import com.esender.search.service.ElasticsearchService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

@Component
@Slf4j
public class SmsWriteLogListener {
    @Autowired
    private ElasticsearchService elasticsearchService;
    @RabbitListener(queues = RabbitMQConstants.SMS_WRITE_LOG)
    public void consume(StandardSubmit standardSubmit, Channel channel, Message message) throws IOException {
        log.info("接收到得存储日志得信息，standardSubmit = {}",standardSubmit);
        // 手动ack确认消息
        elasticsearchService.index(SearchConstant.INDEX_NAME + getYear(), standardSubmit.getSequenceId().toString(), JsonUtil.obj2JSON(standardSubmit));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    public String getYear(){
        return LocalDateTime.now().getYear() + "";
    }
}
